###########################################################################
#	$Id: conceptModule.p 24 2009-05-07 10:57:39Z donbeave $
###########################################################################

@CLASS
conceptModule

@USE
pf/types/pfClass.p
labour/tests/labourAssert.p

@BASE
pfClass

@OPTIONS
locals

#----- Constructor -----

@create[hshOptions]
## hshOptions.cache
## hshOptions.cacheOptions
## hshOptions.templet
## hshOptions.templetOptions
## hshOptions.storageOptions
## hshOptions.dataManagerOptions
## hshOptions.treeBlocks
  ^cleanMethodArgument[hshOptions]

# Response type (default value)
  $self._responseType[html]

  $self._createOptions[$hshOptions]

  ^if(def $hshOptions.cache && $hshOptions.cache is pfCache){
    $self._cache[$hshOptions.cache]
  }{
     $self._cache[]
   }

  ^if(def $hshOptions.templet && $hshOptions.templet is pfTemplet){
    $self._templet[$hshOptions.templet]
  }{
     $self._templet[]
   }

#TODO сделать поддержку специальной локали
  $self._specialLocaleID(255)

  $self._isReady(false)

  $self._defaultStorage[sql]

  $self._storages[
    $.[$_defaultStorage][^defaultStorageFactory[$hshOptions.storageOptions]]]
  ]

# обрабатываемый в данное время объект
  $self._currentObject[]

  $self._initialObject[]

  $self._treeBlocks(^hshOptions.treeBlocks.bool(true))

#----- Public -----

@isReleased[]
  $result(^STORAGE.isReleased[])

@setRelease[blnRelease]
  $result[^STORAGE.setRelease[$blnRelease]]

@isTreeBlocksSupported[]
  $result($_treeBlocks)

@isReady[]
  $result($_isReady)

@registerStorage[strStorageName;objStorageHandler]
  ^labourAssert:required[strStorageName;string]
  ^labourAssert:required[objStorageHandler;labourConceptStorage]

  ^_storages.add[$.[$strStorageName][$objStorageHandler]]

@parseObject[objItem;hshOptions]
  ^labourAssert:required[objItem;conceptObjectItem]

# временная переменная, которая содержит objectItem
  $self._currentObject[$objItem]

  $oBlocksList[^STORAGE.getBlocksByObjectID(^objItem.ID.int(0))]

#TODO возможно заменить на ArrayList
  $oBlocksResults[]

  ^if($oBlocksList is pfArrayList){
    ^oBlocksList.foreach[oBlockItem]{
      $oBlocksResults[${oBlocksResults}^parseBlock[$oBlockItem]]
    }
  }
]

#TODO добавить поиск шаблона ID, type, DEFAULT
  $result[^TEMPLET.render[object_DEFAULT.pt;
    $.vars[
      $.blocksResults[$oBlocksResults]
      $.objectItem[$objItem]

      $.ENGINE[$self]
      $.STORAGE[$STORAGE]
    ]
  ]]

#TODO заменить потом на $objItem.process
  ^if(^objItem.ProcessID.int(0)){
    $oProcess[^STORAGE.getProcessByID(^objItem.ProcessID.int(0))]

    $result[^_shape[$oProcess;
      $.vars[
        $.renderedBlocksData[$result]
        $.objectItem[$objItem]

        $.ENGINE[$self]
        $.STORAGE[$STORAGE]
      ]
    ]]
  }

  $self._currentObject[]

@parseBlock[objBlockItem;hshOptions]
  ^labourAssert:required[objBlockItem;conceptBlockItem]

  $sTempletPath[^_getBlockTempletPath[$objBlockItem]]

# создаем новый объект, т.к. не проходит использование TEMPLET,
# видимо идет замена переменных
#  $oTemplet[^templetFactory[$_createOptions.templetOptions]]
  $oTemplet[$TEMPLET]

  $result[^oTemplet.render[$sTempletPath;
    $.vars[
      $.objectItem[$currentObject]
      $.blockItem[$objBlockItem]

      $.parseBlock[$self.parseBlock]
      $.parseChildBlocks[$self.parseChildBlocks]

      $.ENGINE[$self]
      $.STORAGE[$STORAGE]
    ]
    $.force(true)
  ]]

  ^if(^objBlockItem.ProcessID.int(0)){
    $oProcess[^STORAGE.getProcessByID(^objBlockItem.processID.int(0))]

    $result[^_shape[$oProcess;
      $.vars[
        $.renderedBlockData[$result]
        $.blockItem[$objBlockItem]
        $.ENGINE[$self]
        $.STORAGE[$STORAGE]
      ]
    ]]
  }

@parseChildBlocks[objItem]
  ^labourAssert:required[objItem;conceptBlockItem]

  $oBlocksList[^STORAGE.getBlocksByObjectID($currentObject.ID)[
    $.filter[
      $.parentBlockID[$objItem.ID]
    ]
  ]]

  $result[]

  ^if($oBlocksList is pfArrayList){
    ^oBlocksList.foreach[oBlockItem]{
      $result[${result}^parseBlock[$oBlockItem]]
    }
  }

@createObject[objType;strName;objLocale]
  $result[^STORAGE.createObject[$objType;$strName;$objLocale]]

@createObjectText[altData;objLocale]
  $result[^STORAGE.createObjectText[$altData;$objLocale]]

@assignVar[strVarName;altValue]
  ^TEMPLET.assign[$strVarName;$altValue]

#----- Properties -----

@GET_CACHE[]
  ^if(!def $self._cache){
    $self._cache[^cacheFactory[$_createOptions.cacheType;$_createOptions.cacheOptions]]
  }
  $result[$_cache]

@GET_TEMPLET[]
  ^if(!def $self._templet){
    $self._templet[^templetFactory[$_createOptions.templetOptions]]
  }
  $result[$_templet]

@GET_STORAGE[]
  $result[$_storages.[$_defaultStorage]]

@GET_DATA_MANAGER[]
  ^if(!def $self._dataManager){
    $self._dataManager[^dataManagerFactory[$_createOptions.dataManagerOptions]]
  }
  $result[$_dataManager]

@GET_currentObject[]
  $result[$_currentObject]

@GET_initialObject[]
  $result[$_initialObject]

#----- Private -----

@_getBlockTempletPath[objItem]
  $result[block_DEFAULT.pt]

  ^if(-f "$TEMPLET.templateFolder/block_${objItem.ID}.pt"){
    $result[block_${objItem.ID}.pt]
  }{
    ^if(-f "$TEMPLET.templateFolder/block_${objItem.Name}.pt"){\
      $result[block_${objItem.Name}.pt]
    }{
      ^if(-f "$TEMPLET.templateFolder/blockType_${objItem.dataType.Name}.pt"){
        $result[blockType_${objItem.dataType.Name}.pt]
      }
     }
   }

@_shape[objProcessItem;hshOptions]
  ^labourAssert:required[objProcessItem;conceptProcessItem]

  $sProcessFileName[$objProcessItem.FileName]

  $result[^TEMPLET.render[/processes/$sProcessFileName;$hshOptions]]

#----- Fabriques -----

@cacheFactory[strCacheType;hshCacheOptions]
  ^use[pf/cache/pfCache.p]
  $result[^pfCache::create[$hshCacheOptions]]

@templetFactory[hshTempletOptions]
  ^use[pf/templet/pfTemplet.p]
  $result[^pfTemplet::create[$hshTempletOptions]]

@defaultStorageFactory[hshStorageOptions]
  ^cleanMethodArgument[hshStorageOptions]
  ^use[concept/storages/conceptStorageSQL.p]
  $hshStorageOptions.concept[$self]
  $result[^conceptStorageSQL::create[$hshStorageOptions]]

@dataManagerFactory[hshDataManagerOptions]
  ^cleanMethodArgument[hshDataManagerOptions]
  ^use[concept/management/conceptDataManagerSQL.p]
  $hshDataManagerOptions.concept[$self]
  $result[^conceptDataManagerSQL::create[$hshDataManagerOptions]]
